<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use App\Utils\ModelOperatorTrait;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;

class EntryExitRecordExport implements FromQuery, WithHeadings, WithMapping
{
	use ModelOperatorTrait;

	protected $conditions;

	public function __construct($conditions = []){
		$this->conditions = $conditions;
	}

	protected function model(){
		return app(\App\Models\EntryExitRecord::class);
	}

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
    	$entryExitRecords = $this->queryModel($this->conditions);

    	if(isset($this->conditions['start_time'])){
            if(isset($this->conditions['end_time'])){
                $entryExitRecords = $entryExitRecords->where('readic_datetime','>=',$this->conditions['start_time'])->where('readic_datetime','<=',$this->conditions['end_time']);
            }
        } 

        $entryExitRecords = $entryExitRecords->orderBy('readic_datetime','desc');

        return $entryExitRecords;
    }

    public function map($entryExitRecords):array{
    	return [
    		$entryExitRecords->order_systemnumber,
    		$entryExitRecords->vehicle_number,
    		$entryExitRecords->tare,
    		$entryExitRecords->driver_name,
    		$entryExitRecords->readic_datetime,
    		$entryExitRecords->net_weight,
    		$entryExitRecords->io_status_desc,
    		$entryExitRecords->capvn_path,
    		$entryExitRecords->cap_a_path,
    		$entryExitRecords->cap_b_path,
    		$entryExitRecords->cap_c_path,
    		$entryExitRecords->is_finished_desc,
    	];
    }

    public function headings():array{
    	return [
    		'单据号',
    		'车牌号',
    		'固定皮重',
    		'司机姓名',
    		'刷卡时间',
    		'地磅净重',
    		'出入类型',
    		'车牌抓拍图片',
    		'地磅摄像枪抓拍图片A',
    		'地磅摄像枪抓拍图片B',
    		'地磅摄像枪抓拍图片C（预留）',
    		'是否完成',
    	];
    }
}
